package org.gikolet.ch2.android.widget;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.gikolet.ch2.data.Board;
import org.gikolet.ch2.data.Category;
import org.gikolet.ch2.logic.BoardsLogic;
import org.gikolet.util.Tuple.Tuple2;

import roboguice.inject.ContextScoped;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.google.inject.Inject;
import com.google.inject.name.Named;

@ContextScoped
public class BoardsAdapter extends BaseExpandableListAdapter {

	public Handler updateHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			BoardsAdapter.this.notifyDataSetChanged();
		}
	};

	protected LayoutInflater inflater;

	// private Bitmap blank;

	public static final String BOARDS_LAYOUT_ID = "boardsLayoutId";
	public static final String CATEGORY_TEXT_VIEW_ID = "categoryTextViewId";
	public static final String BOARD_TEXT_VIEW_ID = "boardTextViewId";

	@Inject
	@Named(BOARDS_LAYOUT_ID)
	private int layout;

	@Inject
	@Named(CATEGORY_TEXT_VIEW_ID)
	private int groupTextViewId;

	@Inject
	@Named(BOARD_TEXT_VIEW_ID)
	private int childTextViewId;

	@Inject
	private BoardsLogic boardsLogic;

	private List<Tuple2<Category, List<Board>>> boards;

	@Inject
	public BoardsAdapter(Context context) {
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// this.blank = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
	}

	public Board getChild(int groupPosition, int childPosition) {
		return getBoards().get(groupPosition)._2.get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return getBoards().get(groupPosition)._2.size();
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = this.inflater.inflate(this.layout, parent, false);
		}

		Board child = (Board) this.getChild(groupPosition, childPosition);

		String name = child.getName();

		((TextView) convertView.findViewById(this.childTextViewId))
				.setText(name);

		// ((ImageView) convertView.findViewById(android.R.id.icon))
		// .setImageBitmap(blank);

		return convertView;
	}

	private List<Tuple2<Category, List<Board>>> getBoards() {
		if (this.boards == null) {
			reflush();
		}
		return this.boards;
	}

	public void reflush() {
		try {
			this.boards = this.boardsLogic.getBoards();

		} catch (IOException e) {
			// TODO ここの例外をどうしようか
			e.printStackTrace();

			if (this.boards == null) {
				this.boards = Collections.emptyList();
			}
		}
	}

	public Category getGroup(int groupPosition) {
		return getBoards().get(groupPosition)._1;
	}

	public int getGroupCount() {
		return getBoards().size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = this.inflater.inflate(this.layout, parent, false);
		}

		Category category = (Category) getGroup(groupPosition);

		TextView textView = (TextView) convertView
				.findViewById(this.groupTextViewId);
		textView.setText(category.getName());

		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	@Override
	public void notifyDataSetChanged() {
		reflush();

		super.notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetInvalidated() {
		reflush();

		super.notifyDataSetInvalidated();
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		// this.boardsCache.remove(groupPosition);

		super.onGroupCollapsed(groupPosition);
	}
}
